﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace SMSController
{
    class Rules
    {
        private static string xmlLocation;
        private static XmlNode xnHead;
        private static XmlNode xnAcceptableList;
        private static XmlNode xnShortcuts;
        private static XmlDocument xmlDoc;
        public Rules(string configFileLocation)
        {
            xmlLocation = configFileLocation.Trim();
            initialization();
        }
        private void initialization()
        {
            xmlDoc = new XmlDocument();
            xmlDoc.Load(xmlLocation);
            //head
            xnHead = xmlDoc.GetElementsByTagName(@"head").Item(0);

            //Acceptable list part:
            xnAcceptableList = xmlDoc.GetElementsByTagName(@"acceptableNumber").Item(0);

            //Shortcuts command part:
            xnShortcuts = xmlDoc.GetElementsByTagName(@"shortcuts").Item(0);
        }
        public bool isAll()
        {
            bool isAllvalue = false;
            XmlNode xn = xnAcceptableList.SelectSingleNode(@"isAll");
            isAllvalue = Convert.ToBoolean(xn.InnerText);
            return isAllvalue;
        }
        public bool isAcceptableNumber(string number)
        {
            bool isacceptable=false;
            for (int i=0;i<xnAcceptableList.SelectNodes(@"number").Count;i++)
            {
                XmlNode xn = xnAcceptableList.SelectNodes(@"number").Item(i);
                if (number.Trim().ToLower().IndexOf(xn.InnerText.Trim().ToLower()) >= 0)
                    isacceptable = true;
            }

            return isacceptable;
        }

        public string getHead()
        {
            string head = "";
            head = xnHead.InnerText;
            return head;
        }

        public bool setHead(string strHead)
        {
            bool isSuccess = false;
            xnHead.InnerText = strHead.Trim();
            xmlDoc.Save(xmlLocation);
            isSuccess = true;
            return isSuccess;
        }

        public string getAllAcceptableNumber(string splitter)
        {
            try
            {
                string allAcceptableNumber = "";
                for (int i = 0; i < xnAcceptableList.SelectNodes(@"number").Count; i++)
                {
                    XmlNode xn = xnAcceptableList.SelectNodes(@"number").Item(i);
                    allAcceptableNumber = allAcceptableNumber + xn.InnerText.Trim() + splitter;
                }
                return allAcceptableNumber.Substring(0, allAcceptableNumber.Length - splitter.Length);
            }
            catch
            {
                return "";
            }
        }

        public bool addAcceptableNumber(string number)
        {
            
            bool isSuccess = false;
            if (!isAcceptableNumber(number))
            {
                XmlElement newxe = xmlDoc.CreateElement(@"number");
                newxe.InnerText = number.Trim();
                xnAcceptableList.AppendChild(newxe);
                xmlDoc.Save(xmlLocation);
                isSuccess = true;
                initialization();
            }
            return isSuccess;
        }

        public bool delAcceptableNumber(string number)
        {
            bool isSuccess = false;
            if (isAcceptableNumber(number))
            {
                for (int i = 0; i < xnAcceptableList.SelectNodes(@"number").Count; i++)
                {
                    XmlNode xn = xnAcceptableList.SelectNodes(@"number").Item(i);
                    if (xn.InnerText.Trim().ToLower() == number.Trim().ToLower())
                        xnAcceptableList.RemoveChild(xn);
                }
                xmlDoc.Save(xmlLocation);
                isSuccess = true;
                initialization();
            }
            return isSuccess;
        }


        public bool setIsAll(bool isAllNumber)
        {
            bool isSuccess = false;
            XmlNode xn = xnAcceptableList.SelectSingleNode(@"isAll");
            xn.InnerText = isAllNumber.ToString();
            xmlDoc.Save(xmlLocation);
            isSuccess = true;
            initialization();
            return isSuccess;
        }
        
        public string getShortcutCommand(string shortcutTitle)
        {
            string stringShortcut = "";
            for(int i=0;i<xnShortcuts.ChildNodes.Count;i++)
            {
                XmlNode xnItem = xnShortcuts.ChildNodes[i];
                if (xnItem.SelectSingleNode(@"title").InnerText.Trim().ToLower() == shortcutTitle.Trim().ToLower())
                    stringShortcut = xnItem.SelectSingleNode(@"command").InnerText.Trim();
            }
            return stringShortcut;
        }

        public string getShortcutParameter(string shortcutTitle)
        {
            string stringShortcut = "";
            for (int i = 0; i < xnShortcuts.ChildNodes.Count; i++)
            {
                XmlNode xnItem = xnShortcuts.ChildNodes[i];
                if (xnItem.SelectSingleNode(@"title").InnerText.Trim().ToLower() == shortcutTitle.Trim().ToLower())
                    stringShortcut =xnItem.SelectSingleNode(@"parameter").InnerText.Trim();
            }
            return stringShortcut;
        }

        public string getAllShortcutTitle(string splitter)
        {
            try
            {
                string allShortcutTitle = "";
                for (int i = 0; i < xnShortcuts.ChildNodes.Count; i++)
                {
                    XmlNode xnItem = xnShortcuts.ChildNodes[i];
                    allShortcutTitle = allShortcutTitle + xnItem.SelectSingleNode(@"title").InnerText.Trim() + splitter;
                }
                return allShortcutTitle.Substring(0, allShortcutTitle.Length - splitter.Length);
            }
            catch
            {
                return "";
            }
        }

        public bool addShortcut(string shortcutTitle, string shortcutCommand, string shortcutParameter)
        {
            bool isSuccess = false;
            if(!isShortcut(shortcutTitle.Trim().ToLower()))
            {
                XmlElement newItem = xmlDoc.CreateElement(@"item");
                XmlElement newTitle = xmlDoc.CreateElement(@"title");
                newTitle.InnerText = shortcutTitle.Trim();
                newItem.AppendChild(newTitle);
                XmlElement newCommand = xmlDoc.CreateElement(@"command");
                newCommand.InnerText = shortcutCommand.Trim();
                newItem.AppendChild(newCommand);
                XmlElement newParameter = xmlDoc.CreateElement(@"parameter");
                newParameter.InnerText = shortcutParameter.Trim();
                newItem.AppendChild(newParameter);
                xnShortcuts.AppendChild(newItem);
                xmlDoc.Save(xmlLocation);
                initialization();
                isSuccess = true;
            }
            return isSuccess;
        }

        public bool modifyShortcut(string shortcutTitle, string shortcutCommand, string shortcutParameter)
        {
            bool isSuccess = false;
            if (isShortcut(shortcutTitle.Trim().ToLower()))
            {
                for (int i = 0; i < xnShortcuts.ChildNodes.Count; i++)
                {
                    XmlNode xnItem = xnShortcuts.ChildNodes[i];
                    if (xnItem.SelectSingleNode(@"title").InnerText.Trim().ToLower() == shortcutTitle.Trim().ToLower())
                    {
                        xnItem.SelectSingleNode(@"command").InnerText=shortcutCommand.Trim();
                        xnItem.SelectSingleNode(@"parameter").InnerText =shortcutParameter.Trim();
                    }
                }
                xmlDoc.Save(xmlLocation);
                initialization();
                isSuccess = true;
            }
            return isSuccess;
        }

        public bool delShortcut(string shortcutTitle)
        {
            bool isSuccess = false;
            if (isShortcut(shortcutTitle.Trim().ToLower()))
            {
                for (int i = 0; i < xnShortcuts.ChildNodes.Count; i++)
                {
                    XmlNode xnItem = xnShortcuts.ChildNodes[i];
                    if (xnItem.SelectSingleNode(@"title").InnerText.Trim().ToLower() == shortcutTitle.Trim().ToLower())
                        xnShortcuts.RemoveChild(xnItem);
                }
                xmlDoc.Save(xmlLocation);
                initialization();
                isSuccess = true;
            }
            return isSuccess;
        }

        public bool isShortcut(string shortcutTitle)
        {
            bool isTheShortcut = false;
            for (int i = 0; i < xnShortcuts.ChildNodes.Count; i++)
            {
                XmlNode xnItem = xnShortcuts.ChildNodes[i];
                if (xnItem.SelectSingleNode(@"title").InnerText.Trim().ToLower() == shortcutTitle.Trim().ToLower())
                    isTheShortcut = true;
            }
            return isTheShortcut;
        }
    }
}
